const sequelize = require('sequelize')
const dbfile = require('../db/model/File')

async function saveFile ({destination, filename, path, size, mimetype}) {
  const file = await dbfile.create({ destination, filename, path, size, mimetype })
  return file.dataValues
}

async function findFileList ({ fileids }) {
  var ids = []
  fileids.split(',').forEach(element => {
    ids.push(parseInt(element))
  })
  const FileList = await dbfile.findAll({
    where: {id: {[sequelize.Op.in]: ids}},
    order: [
      ['id', 'desc']
    ]
  })
  return FileList
}

module.exports = {
  saveFile,
  findFileList
}
